import { Entity, PrimaryGeneratedColumn, Column, OneToMany } from "typeorm";
import { ManualTimestamp } from "../../../decorators/manual-timestamp.decorator";
import { CasinoDiceBet } from "./casino-dice-bet.entity";

export enum DiceRoundStatus {
  BETTING = 0, // 下注中
  DRAWING = 1, // 开奖中
  SETTLED = 2, // 已结算
}

export interface DiceResultData {
  num1: number;
  num2: number;
  num3: number;
  sum: number;
  isBig: boolean;
  isSmall: boolean;
  isOdd: boolean;
  isEven: boolean;
  isTriple: boolean;
}

@Entity("casino_dice_rounds")
@ManualTimestamp()
export class CasinoDiceRound {
  @PrimaryGeneratedColumn()
  id!: number;

  @Column({
    name: "start_time",
    type: "datetime",
    comment: "开始时间",
  })
  startTime!: Date;

  @Column({
    name: "end_time",
    type: "datetime",
    comment: "结束时间",
  })
  endTime!: Date;

  @Column({
    type: "tinyint",
    default: DiceRoundStatus.BETTING,
    comment: "状态",
  })
  status!: DiceRoundStatus;

  @Column({ type: "tinyint", nullable: true, comment: "骰子1点数" })
  num1!: number | null;

  @Column({ type: "tinyint", nullable: true, comment: "骰子2点数" })
  num2!: number | null;

  @Column({ type: "tinyint", nullable: true, comment: "骰子3点数" })
  num3!: number | null;

  @Column({ type: "tinyint", nullable: true, comment: "骰子总和" })
  sum!: number | null;

  @Column({
    name: "is_big",
    type: "tinyint",
    nullable: true,
    comment: "是否为大",
  })
  isBig!: number | null;

  @Column({
    name: "is_small",
    type: "tinyint",
    nullable: true,
    comment: "是否为小",
  })
  isSmall!: number | null;

  @Column({
    name: "is_odd",
    type: "tinyint",
    nullable: true,
    comment: "是否为单",
  })
  isOdd!: number | null;

  @Column({
    name: "is_even",
    type: "tinyint",
    nullable: true,
    comment: "是否为双",
  })
  isEven!: number | null;

  @Column({
    name: "is_triple",
    type: "tinyint",
    nullable: true,
    comment: "是否为豹子",
  })
  isTriple!: number | null;

  @Column({
    name: "total_gold",
    type: "bigint",
    default: 0,
    comment: "总金币下注",
  })
  totalGold!: number;

  @Column({
    name: "total_silver",
    type: "bigint",
    default: 0,
    comment: "总银币下注",
  })
  totalSilver!: number;

  @Column("datetime", { name: "created_at", comment: "创建时间" })
  created_at!: Date;

  @Column("datetime", {
    name: "updated_at",
    nullable: true,
    comment: "更新时间",
  })
  updated_at!: Date | null;

  // 关联关系 - 移除外键约束，只保留字段引用
  // // @OneToMany => CasinoDiceBet, (bet: CasinoDiceBet) => bet.round)
  // // diceBets!: CasinoDiceBet[];

  // 计算属性
  get isActive(): boolean {
    return this.status === DiceRoundStatus.BETTING;
  }

  get isDrawing(): boolean {
    return this.status === DiceRoundStatus.DRAWING;
  }

  get isSettled(): boolean {
    return this.status === DiceRoundStatus.SETTLED;
  }

  get remainingTime(): number {
    if (this.isSettled) return 0;
    const now = new Date();
    const end = new Date(this.endTime);
    return Math.max(0, Math.floor((end.getTime() - now.getTime()) / 1000));
  }

  get totalBetAmount(): number {
    return this.totalGold + this.totalSilver;
  }

  // 类型安全的获取开奖结果方法
  getDiceResult(): DiceResultData | null {
    if (!this.num1 || !this.num2 || !this.num3) {
      return null;
    }

    return {
      num1: this.num1,
      num2: this.num2,
      num3: this.num3,
      sum: this.sum || this.num1 + this.num2 + this.num3,
      isBig: Boolean(this.isBig),
      isSmall: Boolean(this.isSmall),
      isOdd: Boolean(this.isOdd),
      isEven: Boolean(this.isEven),
      isTriple: Boolean(this.isTriple),
    };
  }
}
